﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;

public class RuntimeLog : MonoBehaviour {

    static RuntimeLog instance;

    List<string> logData = new List<string>();
    //StringBuilder logDataSB = new StringBuilder();
    int logCount = 0;
    int startLogCount = 0;
    public UILabel m_LogLabel;
    public GameObject m_LogBody;
    public UIScrollView m_ScrollPanel;

    static public RuntimeLog Instance
    {
        get
        {
            if (instance == null)
                CreateRuntimeLog();

            return instance; 
        }
        set { instance = value;}
    }

    static void CreateRuntimeLog()
    {
        GameObject preFab = Resources.Load("Prefabs/RuntimeLog/RuntimeLog") as GameObject;
        preFab = Instantiate(preFab) as GameObject;

        instance = preFab.GetComponent<RuntimeLog>();
        DontDestroyOnLoad(preFab);

        instance.SetPosition();

        instance.m_LogBody.SetActive(false);
    }

	public static void Clear()
	{
		Instance.logData.Clear();
		Instance.logCount=0;
	}

    static public void Log(object log)
    {
		Instance.logData.Add(string.Format("[{0:0000} :{1:00} {2:000}] [ffffff]{3}[-]\n", Instance.logCount, DateTime.Now.Second, DateTime.Now.Millisecond,log));
        Instance.logCount++;

        UpdateLogBoard();
    }

    static public void LogError(object log)
    {
		Instance.logData.Add(string.Format("[{0:0000} :{1:00} {2:000}] [ffffff]{3}[-]\n", Instance.logCount, DateTime.Now.Second, DateTime.Now.Millisecond,log));
        Instance.logCount++;

        UpdateLogBoard();
    }

    static public void LogWarning(object log)
    {
		Instance.logData.Add(string.Format("[{0:0000} :{1:00} {2:000}] [ffffff]{3}[-]\n", Instance.logCount, DateTime.Now.Second, DateTime.Now.Millisecond,log));
        Instance.logCount++;

        UpdateLogBoard();
    }

    static public void LogCustom(object log, Color color)
    {
        string temp1 = string.Format("{0:X}", (int)(color.r * 255.0f));       
        if (temp1.Length == 1)
            temp1 = temp1.Insert(0, "0");

        string temp2 = string.Format("{0:X}", (int)(color.g * 255.0f));
        if (temp2.Length == 1)
            temp2 = temp2.Insert(0, "0");

        string temp3 = string.Format("{0:X}", (int)(color.b * 255.0f));
        if (temp3.Length == 1)
            temp3 = temp3.Insert(0, "0");

		Instance.logData.Add(string.Format("[{4:0000} :{5:00} {6:000}] [{0}{1}{2}]{3}[-]\n", temp1, temp2, temp3, log, Instance.logCount, DateTime.Now.Second, DateTime.Now.Millisecond));
        Instance.logCount++;

        UpdateLogBoard();
    }

    static void UpdateLogBoard()
    {
        if (Instance.m_LogBody.activeSelf)
        {
            Instance.isNeedUpdate = true;
        }
    }

    bool isNeedUpdate = false;
    void Update()
    {
        if (isNeedUpdate)
        {
            isNeedUpdate = false;

            if (logData.Count > 200)
            {
                int removeCount = logData.Count - 200;
                logData.RemoveRange(0, removeCount);
            }

            StringBuilder logDataSB = new StringBuilder();
            foreach (string st in logData)
            {
                logDataSB.Append(st);
            }

            m_LogLabel.text = logDataSB.ToString();

            Vector2 dimensions = m_LogLabel.localSize;

            BoxCollider coll = m_LogLabel.GetComponent<BoxCollider>();
            coll.center = new Vector3(250, dimensions.y / 2.0f, 0);
            coll.size = dimensions;

            //Vector3 pos = Instance.m_ScrollPanel.transform.localPosition;
            //pos.y = 0;
            //Instance.m_ScrollPanel.transform.localPosition = pos;
        }
    }

    static public void ShowLogBoard()
    {
        if (!Instance.m_LogBody.activeSelf)
        {
            Instance.SetPosition();
            Instance.m_LogBody.SetActive(true);
        }

       UpdateLogBoard();
    }

    static public void HideLogBaord()
    {
        Instance.m_LogBody.SetActive(false);
    }

    void OnX(GameObject obj)
    {
        HideLogBaord();
    }

    void OnOn(GameObject obj)
    {
        ShowLogBoard();
    }

    void SetPosition()
    {
        Instance.transform.parent = GameObject.Find("UI Root (2D)/Camera").transform;
        Instance.transform.localScale = Vector3.one;
        Instance.transform.localPosition = new Vector3(0, 0, -5.0f);
        Instance.transform.parent = null;
    }

    void OnLevelWasLoaded()
    {
        Camera cam = GameObject.Find("UI Root (2D)/Camera").camera;
        cam.cullingMask |= 1 << gameObject.layer;

        UICamera uicam = cam.GetComponent<UICamera>();
        uicam.eventReceiverMask |= 1 << gameObject.layer;

        SetPosition();
    }
}
